package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TodayBestService {
    @Autowired
    private UserService userService;
    @Autowired
    private RecommendUserService recommendUserService;
    @Value("${tanhua.sso.default.user}")//默认推荐的用户id
    private Long defaultUserId;
    @Autowired
    private UserInfoService userInfoService;

    public TodayBest queryTodayBest() {
        //无需校验  直接获取 user对象
        User user = UserThreadLocal.get();
        if (null == user) {
            //token 无效
            return null;
        }
        //运行到这里说明token有效 调用RecommendUserService 他是来负责服务器交互的
        TodayBest todayBest = this.recommendUserService.queryTodayBest(user.getId());
        if (todayBest == null) {
            //没有查到推荐用户     给出默认推荐用户
            todayBest = new TodayBest();
            //把默认id赋值给todayBest
            todayBest.setId(defaultUserId);
            todayBest.setFateValue(86l);
        }
        //补全个人信息  这里应该是传递todayBaset里面的id 获取这个id的数据
        UserInfo userInfo = this.userInfoService.queryUserInfoByUserId(todayBest.getId());
        if (userInfo == null) {
            return null;
        }
        todayBest.setAvatar(userInfo.getLogo());
        todayBest.setAge(userInfo.getAge());
        todayBest.setNickname(userInfo.getNickName());
        //切割tags
        todayBest.setTags(userInfo.getTags().split(","));
        todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
        return todayBest;
    }

    public PageResult queryRecommendation(RecommendUserQueryParam queryParam) {
        //校验token
        User user = UserThreadLocal.get();
        if (null == user) {
            return null;
        }
        PageResult pageResult = new PageResult();
        pageResult.setPage(queryParam.getPage());
        pageResult.setPagesize(queryParam.getPagesize());

        PageInfo<RecommendUser> pageInfo = this.recommendUserService.queryRecommendUserList(user.getId()
                , queryParam.getPage(), queryParam.getPagesize());
        List<RecommendUser> records = pageInfo.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            //没有查到推荐用户用表
            return pageResult;
        }
        //查询到推荐用户列表 补充个人信息
        // 收集符合条件的用户id  使用set集合去重复
        Set<Long> userIds = new HashSet<>();
        for (RecommendUser record : records) {
            userIds.add(record.getUserId());
        }
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIds);
        //查询按钮的功能  添加查询条件
        if (StringUtils.isNotEmpty(queryParam.getGender())) {
            //需要性别参数查询 StringUtils.equals(queryParam.getGender(),"man")?1:2
            queryWrapper.eq("sex", StringUtils.equals(queryParam.getGender(), "man") ? 1 : 2);
        }
        if (StringUtils.isNotEmpty(queryParam.getCity())) {
            //需要城市参数查询
            queryWrapper.like("city", queryParam.getCity());
        }
        //年龄参数 条件  小于等于
        if (queryParam.getAge() != null) {
            queryWrapper.le("age", queryParam.getAge());
        }
        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoList(queryWrapper);
        if (CollectionUtils.isEmpty(userInfoList)) {
            //没有推荐用户满足这些条件
            return pageResult;
        }
        List<TodayBest> todayBests = new ArrayList<>();
        //满足查询条件查询的用户 用todaybest 封装起来
        for (UserInfo userInfo : userInfoList) {
            TodayBest todayBest = new TodayBest();
            todayBest.setId(userInfo.getId());
            todayBest.setAvatar(userInfo.getLogo());
            todayBest.setNickname(userInfo.getNickName());
            todayBest.setTags(StringUtils.split(userInfo.getTags(), ","));
            todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
            todayBest.setAge(userInfo.getAge());
            //缘分值
            for (RecommendUser record : records) {
                if (record.getUserId().longValue() == userInfo.getUserId()) {
                    //对缘分值取整
                    double score = Math.floor(record.getScore());
                    todayBest.setFateValue(Double.valueOf(score).longValue());
                    break;
                }
            }
            todayBests.add(todayBest);
        }
//按照缘分值进行倒叙排序
        Collections.sort(todayBests, (o1, o2) ->
                new Long(o2.getFateValue() - o1.getFateValue()).intValue());
        pageResult.setItems(todayBests);
        return pageResult;
    }
}
